package es.project.sien7e.main.services;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.validation.Valid;

import org.springframework.stereotype.Service;

import es.project.sien7e.main.PMF;
import es.project.sien7e.main.model.Provincia;

@Service
public class ProvinciaServiceImpl implements ProvinciaService {

	@SuppressWarnings("unchecked")
	@Override
	public List<Provincia> getProvincias() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Provincia.class);
		query.setOrdering("provincia asc, id asc");
		return (List<Provincia>) query.execute();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Provincia> getListadoProvincias() throws SQLException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Provincia> result = new ArrayList<Provincia>();
		List<Provincia> list = null;
		Provincia reg1 = new Provincia();
		reg1.setId((long) 0);
		result.add(reg1);
		Query query = pm.newQuery(Provincia.class);
		query.setOrdering("provincia asc, id asc");
		list = (List<Provincia>) query.execute();
		result.addAll(list);
		return list;
	}

	@Override
	public Provincia grabaProvincia(@Valid Provincia obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Provincia obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Provincia insertaProvincias(@Valid Provincia obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Provincia obj2 = null;
		try {
			obj2 = pm.makePersistent(obj);
		} finally {
			pm.close();
		}
		return obj2;
	}

	@Override
	public Integer borrarProvincias(@Valid Provincia obj) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Integer val = 0;
		try {
			pm.deletePersistent(obj);
			val = 1;
		} finally {
			pm.close();
		}
		return val;
	}
}
